#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/JOB/lib -I/data/Software/mydan/JOB/private/lib
use strict;
use warnings;
use MIME::Base64;
use Code;
use Logs;

$| ++;

=head1 SYNOPSIS

    #
    db => $db,

    'submitter' => 'open-c3',
    'user' => 'open-c3',
    'cont' => 'cont',
    'environment' => {
                       'isCronFailEmail' => 'true',
                       'isApiFailEmail' => 'true'
                     }


=cut

return sub
{
    my %param = @_;

    my ( $db, $logs ) = @param{qw( db logs )};
    $logs = Logs->new( 'code.notifyapproval' ) unless $logs;

    my $perfix = "isApprovalSuccess";

    my $status = 'skip';
    for my $type ( qw( email sms ) )
    {
        my $tmptype = $type; $tmptype =~ s/^[a-z]/\u$&/;
        my $keyname = "${perfix}${tmptype}";

        next unless $param{environment} && ref $param{environment} eq 'HASH' 
            && $param{environment}{$keyname} && $param{environment}{$keyname} eq 'true';

        my $s = eval{ Code->new( "notify.plugin/$type" )->run( %param, to => [ $param{user} ], notifytype => 'approval' ); };
        my $x = $@;
        if( $@ )
        {
            $logs->err( "notify.plugin/$type fail uuid=$param{uuid} $x" );
            $status = 'fail';
        }
        $status = 'done' if $status eq 'skip' && $s;
        $status = 'fail' unless $s;
    }

    return $status;
}
